//
//  JMVistorView.swift
//  sinaWeibo
//
//  Created by Yahua on 15/12/16.
//  Copyright © 2015年 itcast. All rights reserved.
//


//一开始要用init frame 方法  初始化里面所有的控件的时候调用 一般在uiview上调用
//接着懒加载当前控件爱你并且带代码段里面给控件赋值
//接着把控件添加在父控件中  并且代码创建他们的约束
//把(控件添加在父控件中  并且代码创建他们的约束)步骤封装起来

//如何让这个页面显示出来?  应该判断一下用户有没有登录的时候 有就不显示 没有就显示  但是tabbarController一开始就把所有的tableView都加载出来了  我们要去每一个tableView里面判断  这样tableView才不会显示出来  这样的话就太繁琐了  所以我们自定义一个所有显示的控制器的父类  在父类上判断 让父类告诉他们要不要显示出来   JMBaseViewController
/*
   class JMHomeViewController: JMBaseViewController
*/

import UIKit

class JMVistorView: UIView
{

    
    //用init frame 方法 初始化里面所有的控件
    
    override init(frame: CGRect)
    {
        
        super.init(frame: frame)
        
        prepareUI()
        
    }
    
    
    //系统推荐一起配套使用
    required init?(coder aDecoder: NSCoder)
    {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    //封装View控件   //要添加控件 要先创建控件 然后懒加载 在创建
    
    private func prepareUI()
    {
        //添加子控件
        addSubview(iconView)
        addSubview(homeView)
        addSubview(messageLable)
        addSubview(registerBtn)
        addSubview(loginBtn)
        
        //autoresizing会用影响autolayout的约束   
         iconView.translatesAutoresizingMaskIntoConstraints = false
         homeView.translatesAutoresizingMaskIntoConstraints = false
         messageLable.translatesAutoresizingMaskIntoConstraints = false
         registerBtn.translatesAutoresizingMaskIntoConstraints = false
         loginBtn.translatesAutoresizingMaskIntoConstraints = false
        
        //设置位置
        //使用autolayout来布局
        
        //约束转轮centerX和父空间的centerX重合
        //item 要余数的view
        //attribute:要约束的属性
        //relatedBy:要约束的状态
        //toitem 要相对雨哪一个view余数
        
        //转轮.x
        self.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        //转轮.y
         self.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: -40))
        //房子.xhomeView
        self.addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        //房子.y
        self.addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        //lable.x
         self.addConstraint(NSLayoutConstraint(item: messageLable, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        //lable.y
          self.addConstraint(NSLayoutConstraint(item: messageLable, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 20))
        
        //注册按钮.width
        self.addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        
        //注册按钮的高度
        self.addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        
        //注册的按钮的左边
        self.addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Left , relatedBy: NSLayoutRelation.Equal, toItem: messageLable , attribute: NSLayoutAttribute.Left , multiplier: 1, constant: 0))
        
        //注册按钮距离Lable的距离
        
        self.addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Top , relatedBy: NSLayoutRelation.Equal, toItem: messageLable, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 20))
        
        
        //登陆按钮.width
        self.addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        
        //登陆按钮的高度
        self.addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        
        //登陆的按钮的右边
        self.addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Right , relatedBy: NSLayoutRelation.Equal, toItem: messageLable , attribute: NSLayoutAttribute.Right , multiplier: 1, constant: 0))
        
        //登陆按钮距离Lable的距离
        
        self.addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Top , relatedBy: NSLayoutRelation.Equal, toItem: messageLable, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 20))
        
        
        
        
        
        
    }
    
    
    
    
    // MARK: -------------------------懒加载
    //转轮
    private lazy var iconView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    //房子
    private lazy var homeView = UIImageView(image: (UIImage(named: "visitordiscover_feed_image_house")))
    
    //消息
    private lazy var messageLable : UILabel = {
        
        let lable = UILabel()
        
        lable.text = "关注一些人,看看这里有什么惊喜!"
        
        lable.textColor = UIColor.grayColor()
        
        return lable
        
    }()
    
    //注册按钮
    private lazy var registerBtn : UIButton = {
       
        let btn = UIButton()
        
        btn.setTitle("注册", forState: UIControlState.Normal)
        
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        return btn
        
    }()
    
    //登陆按钮
    private lazy var loginBtn : UIButton = {
       
        let btn = UIButton()
        
        btn.setTitle("登陆", forState: UIControlState.Normal)
        
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        return btn

    }()
    
    
    


}
